package com.hongkzh.www.other.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.bumptech.glide.Glide;
import com.hongkzh.www.R;
import com.hongkzh.www.other.utils.UIUtils;

import static android.support.v7.widget.RecyclerView.SCROLL_STATE_DRAGGING;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_IDLE;
import static android.support.v7.widget.RecyclerView.SCROLL_STATE_SETTLING;

public abstract class OnVerticalScrollListener extends RecyclerView.OnScrollListener {
    private int mdy = 0;
    private boolean isVisible = false;
    private int height = 0;

    public void setHeight(int height) {
        this.height -= height;
    }

    @Override
    public final void onScrolled(RecyclerView recyclerView, int dx, int dy) {
        if (!recyclerView.canScrollVertically(-1)) {
            onScrolledToTop();
        } else if (!recyclerView.canScrollVertically(1)) {
            onScrolledToBottom();
        } else if (dy < 0) {
            onScrolledUp();
        } else if (dy > 0) {
            onScrolledDown();
        }
        mdy += dy;
        if (height <= 0) {
            height += recyclerView.getChildAt(0).getMeasuredHeight();
            height += recyclerView.getChildAt(1).getMeasuredHeight();
        }
        if (height <= mdy) {
            isVisible = true;
            onScrolledVisible();
        } else {
            if (isVisible) {
                isVisible = false;
                onScrolledGone();
            }
        }
    }

    @Override
    public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
        super.onScrollStateChanged(recyclerView, newState);
        switch (newState) {
            case SCROLL_STATE_IDLE:
                Glide.with(recyclerView.getContext()).resumeRequests();
                break;
            case SCROLL_STATE_DRAGGING:
            case SCROLL_STATE_SETTLING:
                Glide.with(recyclerView.getContext()).pauseRequests();
                break;
        }
    }

    public void onScrolledUp() {
    }

    public void onScrolledDown() {
    }

    public void onScrolledToTop() {
    }

    public void onScrolledToBottom() {
    }

    public void onScrolledVisible() {
    }

    public void onScrolledGone() {
    }
}